package com.briup.estore.service.impl;

import com.briup.estore.common.constant.ExceptionMsgEnum;
import com.briup.estore.common.exception.EstoreException;
import com.briup.estore.common.pojo.Book;
import com.briup.estore.common.pojo.Collect;
import com.briup.estore.common.utils.BeanCopyUtils;
import com.briup.estore.common.vo.CollectVO;
import com.briup.estore.dao.BookDao;
import com.briup.estore.dao.CollectDao;
import com.briup.estore.dao.impl.BookDaoImpl;
import com.briup.estore.dao.impl.CollectDaoImpl;
import com.briup.estore.service.CollectService;

import java.util.List;

/**
 * @Author briup-adam
 * @Date 2023/8/3 下午2:21
 * @Description
 **/

public class CollectServiceImpl implements CollectService {
    private CollectDao collectDao = new CollectDaoImpl();
    private BookDao bookDao = new BookDaoImpl();

    @Override
    public List<CollectVO> findCollects(Integer userId) {
        List<Collect> list = collectDao.findByUserId(userId);
        List<CollectVO> collectVOS = BeanCopyUtils.copyList(list, CollectVO.class);
        collectVOS.forEach(i -> {
            Book book = bookDao.selectById(i.getId());
            i.setCover(book.getCover());
            i.setPrice(book.getPrice());
            i.setName(book.getName());
        });
        return collectVOS;
    }

    @Override
    public void addCollect(Integer userId, Integer bookId) {
        Collect collect = collectDao.findByUserIdAndBookId(userId, bookId);
        if (collect == null)
            collectDao.save(userId, bookId);
        else
            throw new EstoreException(ExceptionMsgEnum.COLLECT_BOOK_IS_EXIST);
    }

    @Override
    public void deleteCollect(int... ids) {
        for (int id : ids) {
            collectDao.deleteById(id);

        }

    }
}
